﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using System.ComponentModel.Composition;
using Caliburn.Micro;
using OI.Events;
using OI.Framework;
using OI.Views;

namespace OI.ViewModels
{
    [Export(typeof(SearchPanelViewModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class SearchPanelViewModel : Screen, IHandle<ConfigureSearchBox>
    {
        /// <summary>
        /// Search string - bound by convention
        /// </summary>
        public string Search
        {
            get { return _Search; }
            set
            {
                if (_Search != value && value != null)
                {
                    Events.Publish(new ChangeSearchText()
                    {
                        SearchString = value
                    });
                    _Search = value;
                }
            }
        }
        private string _Search;
        private WatermarkTextBox SearchTextbox;

        public string Watermark
        {
            get
            {
                if (SearchTextbox != null)
                {
                    return SearchTextbox.Watermark;
                }
                return "";
            }
            set
            {
                _Watermark = value;
                if (SearchTextbox != null)
                {
                    SearchTextbox.Watermark = _Watermark;
                }
            }
        }
        private string _Watermark;


        public bool IsEnabled
        {
            get
            {
                if (SearchTextbox != null)
                {
                    return SearchTextbox.IsEnabled;
                }
                return false;
            }
            set
            {
                _IsEnabled = value;
                if (SearchTextbox != null)
                {
                    SearchTextbox.IsEnabled = _IsEnabled;
                }
            }
        }
        private bool _IsEnabled;

        readonly IEventAggregator Events;


        [ImportingConstructor]
        public SearchPanelViewModel(IEventAggregator events)
        {
            Events = events;
            Events.Subscribe(this);
        }


        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);
            SearchTextbox = ((SearchPanelView)view).Search;
            SearchTextbox.IsEnabled = _IsEnabled;
            SearchTextbox.Watermark = _Watermark;
        }


        public void Handle(ConfigureSearchBox message)
        {
            Watermark = message.Watermark;
            IsEnabled = message.IsEnabled;
            
            // set the initial test without broadcasting a text change
            if (message.InitialText != null)
            {
                _Search = message.InitialText;

                //also broadcast event that the search text was changed
                Events.Publish(new ChangeSearchText()
                {
                    SearchString = message.InitialText
                });
            }

            NotifyOfPropertyChange(() => Search);
        }
    }
}
